package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ArticleTypeMapper;
import com.think.teaching.model.ArticleType;
import com.think.teaching.service.ArticleTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资源类型 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ArticleTypeServiceImpl extends BaseServiceImpl<ArticleTypeMapper, ArticleType> implements ArticleTypeService {

	@Override
	public ArticleType InsetArticleType(ArticleType ArticleType) {
		if (this.save(ArticleType)) {
			return ArticleType;
		}
		return null;
	}


	@Override
	public ArticleType UpdateArticleType(ArticleType ArticleType) {
		if (this.updateById(ArticleType)) {
			return ArticleType;
		}
		return null;
	}

	@Override
	public ArticleType UserSelectArticleType(Integer id,Boolean withoutBan) {
		QueryWrapper<ArticleType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return getOne(queryWrapper);
	}
	
	public ArticleType SelectArticleTypeWithName(String name,Boolean withoutBan) {
		QueryWrapper<ArticleType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("name", name).ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return getOne(queryWrapper);
	}
	
	@Override
	public IPage<ArticleType> ListArticleType(Integer page,Integer count,Boolean withoutBan){
		QueryWrapper<ArticleType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<ArticleType> ipage = new Page<>(page, count);
		return this.page(ipage,queryWrapper);
	}
	
	public List<ArticleType> ListArticleTypeSearch(String key,Boolean withoutBan){
		QueryWrapper<ArticleType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return this.list(queryWrapper);
	}
	
	public ArticleType SetArticleState(Integer id, Integer state) {
		UpdateWrapper<ArticleType> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

}
